package com.py.credit.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.AuthorizeLog;
import com.py.credit.service.AppAllService;
import com.py.framework.core.annotation.IgnoreAuthority;
import com.py.framework.core.common.Page;

@RestController
@RequestMapping("/appAll")
public class AppAllController {
	
	@Autowired	
	private AppAllService appAllService;
	/**
	 * 查询订阅应用
	 * @param dept
	 * @param model
	 * @param company
	 * @param page
	 * @return
	 */
	@IgnoreAuthority
    @RequestMapping(value = "/selectDY.json")
    public RestServiceResult selectDY(
             String userid,
             String type)
	{
		RestServiceResult result = new RestServiceResult();
		try {
			List<String> resultList=new ArrayList<String>();
			String ids=appAllService.selectDY(userid, type);
			if(!StringUtils.isEmpty(ids)){
				resultList=java.util.Arrays.asList(ids.split(","));
			}		
			result.setData(resultList);
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			ExceptionLogger.error("AppAllController#selectDY", "查询订阅应用！ERROR:" + e.getMessage(), e);
			return result;
		}
    	
    	return result;
    }
    /**
     * 添加和删除订阅应用
     * @param userid
     * @param type
     * @param dyidlist
     * @return
     */
	@IgnoreAuthority
    @RequestMapping(value = "/addDY.do")
    public RestServiceResult addDY(
            @RequestParam(value ="userid", defaultValue = "", required = false) String userid,
            @RequestParam(value ="type", defaultValue = "", required = false) String type,
            @RequestParam(value ="dyidlist", defaultValue = "", required = false) String dyidlist) {
		RestServiceResult result = new RestServiceResult();
		try {
			appAllService.deleteByIdType(userid, type);
	    	appAllService.addDY(type, userid, dyidlist);
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			ExceptionLogger.error("AppAllController#addDY", "app订阅应用！ERROR:" + e.getMessage(), e);
			return result;
		}
    	
    	return result;
    }
	

	@IgnoreAuthority
	@RequestMapping(value = "/getQypj.json")
	public RestServiceResult<Map<String, Object>> getQypj(
			@RequestParam(value = "qymc", defaultValue = "", required = true) String qymc) {
		
		Map<String, Object> listDetail = new HashMap<>();
		
		Map<String, Object> map0 = appAllService.getQypj(qymc,this.getLast12Months(0));
		if(map0 == null){
			map0 = new HashMap<>();
			map0.put("qymc", qymc);
			map0.put("date", this.getLast12Months(0));
			map0.put("score", 0);
			map0.put("hyscore", 0);
		}
		Map<String, Object> map1 = appAllService.getQypj(qymc,this.getLast12Months(1));
		if(map1 == null){
			map1 = new HashMap<>();
			map1.put("qymc", qymc);
			map1.put("date", this.getLast12Months(1));
			map1.put("score", 0);
			map1.put("hyscore", 0);
		}
		Map<String, Object> map2 = appAllService.getQypj(qymc,this.getLast12Months(2));
		if(map2 == null){
			map2 = new HashMap<>();
			map2.put("qymc", qymc);
			map2.put("date", this.getLast12Months(2));
			map2.put("score", 0);
			map2.put("hyscore", 0);
		}
		Map<String, Object> map3 = appAllService.getQypj(qymc,this.getLast12Months(3));
		if(map3 == null){
			map3 = new HashMap<>();
			map3.put("qymc", qymc);
			map3.put("date", this.getLast12Months(3));
			map3.put("score", 0);
			map3.put("hyscore", 0);
		}
		Map<String, Object> map4 = appAllService.getQypj(qymc,this.getLast12Months(4));
		if(map4 == null){
			map4 = new HashMap<>();
			map4.put("qymc", qymc);
			map4.put("date", this.getLast12Months(4));
			map4.put("score", 0);
			map4.put("hyscore", 0);
		}
		Map<String, Object> map5 = appAllService.getQypj(qymc,this.getLast12Months(5));
		if(map5 == null){
			map5 = new HashMap<>();
			map5.put("qymc", qymc);
			map5.put("date", this.getLast12Months(5));
			map5.put("score", 0);
			map5.put("hyscore", 0);
		}
		
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		list.add(map0);
		list.add(map1);
		list.add(map2);
		list.add(map3);
		list.add(map4);
		list.add(map5);
		
		listDetail.put("tableList", list);
		return new RestServiceResult<Map<String, Object>>(listDetail);
	}
	
	
	private String getLast12Months(int i) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.MONTH, -i);
		Date m = c.getTime();
		return sdf.format(m);
	}
}
